var t = require("../../utils/api.js"), e = require("../../utils/disapi.js"), a = require("../../utils/common.js"), o = getApp();

require("../../utils/qqmap-wx-jssdk.min.js");

Page({
    data: {
        value: "",
        active: "",
        recommend: [],
        // authorize: !1,
        location: !1,
        city: "北京市",
        url: "/pages/template/index",
        switch: "switchTab",
        history: [],
        type: "",
        showLoding: !0,
        showPrevLoading: !1,
        loadingwait: !1,
        showContent: !0,
        shareEnter: !1,
        refresh: !1,
        diy_community_name: o.globalData.diy_community_name
    },
    onLoad: function(e) {
        var a = this;
        a.setData({
            diy_community_name: o.globalData.diy_community_name
        }), wx.getStorage({
            key: "share_scene",
            success: function(t) {
                a.setData({
                    shareEnter: !0,
                    shareScene: t.data.scene,
                    shareCount: t.data.count,
                    url: "/pages/template/index?scene=" + t.data.scene + "&&count=" + t.data.count
                });
            }
        }), console.log(a.data.shareScene), a.setData({
            showPrevLoading: !a.data.showPrevLoading,
            loadingwait: !0
        }), 10 == e.type && a.setData({
            type: 10
        });
        var n = e.p_id ? e.p_id : "";
        "" != n && void 0 != n && wx.setStorage({
            key: "pid",
            data: n
        }), wx.setNavigationBarColor({
            frontColor: wx.getStorageSync("basic").topcolor,
            backgroundColor: wx.getStorageSync("basic").topbg
        }), "" != o.globalData.share_url && void 0 != o.globalData.share_url && a.setData({
            url: o.globalData.share_url,
            switch: "navigate"
        }), wx.getLocation({
            type: "wgs84",
            success: function(n) {
                var i = {
                    lat: n.latitude,
                    lng: n.longitude,
                    types: 20
                };
                if (o.globalData.latitude = n.latitude, o.globalData.longitude = n.longitude, "selected" == e.act) {
                    console.log(e);
                    var s = e.city, c = e.cid;
                    a.setData({
                        city: s,
                        cid: c
                    });
                } else {
                    var n = a.getLocal(i.lat, i.lng);
                    console.log(n);
                    c = "";
                }
                t.getNearestHead({
                    lat: i.lat,
                    lng: i.lng,
                    cid: c,
                    openid: o.globalData.openid
                }).then(function(e) {
                    console.log(e), 0 == e.errno && 0 !== e.data.data.length && setTimeout(function() {
                        console.log(3), a.setData({
                            showPrevLoading: !a.data.showPrevLoading,
                            loadingwait: !0,
                            showContent: !1
                        });
                    }, 1e3), 1 == e.errno && setTimeout(function() {
                        console.log(1), a.setData({
                            loadingwait: !1,
                            showContent: !1
                        });
                    }, 1e3), console.log(e.data.data), o.globalData.location = !0;
                    var n = e.data.data, i = e.data.history;
                    console.log(i);
                    for (var s = [], c = 0; c < n.length; c++) n[c].m = parseFloat(n[c].m / 1e3).toFixed(2), 
                    s.push(n[c]);
                    a.setData({
                        location: !0,
                        history: i
                    }), setTimeout(function() {
                        a.setData({
                            recommend: s
                        });
                    }, 1e3), t.getTeamOpenid({
                        openid: o.globalData.openid
                    }).then(function(t) {
                        console.log(t), 0 == t.errno && "" != t.data.head_info && wx.setStorageSync("head_info", t.data.head_info), 
                        wx.getStorageSync("head_info") ? (a.setData({
                            active: wx.getStorageSync("head_info")
                        }), console.log(a.data.active)) : (a.setData({
                            active: t.data.head_info
                        }), console.log(a.data.active));
                    });
                }), e.entertype && 1 == e.entertype && (console.log(2222)
                // ,wx.getSetting({
                //     success: function(e) {
                //         void 0 !== e.authSetting["scope.userInfo"] ? wx.getUserInfo({
                //             success: function(e) {
                //                 var a = {
                //                     avatarUrl: e.userInfo.avatarUrl,
                //                     nickName: e.userInfo.nickName,
                //                     openid: wx.getStorageSync("openid"),
                //                     sessionkey: sessionkey
                //                 };
                //                 console.log(a), t.inserUserdata(a).then(function(t) {
                //                     console.log(t);
                //                 });
                //             },
                //             fail: function() {
                //                 console.log(444);
                //                 var t = a.route;
                //                 console.log(t), wx.reLaunch({
                //                     url: "/pages/authorization / authorization ? url =" + t
                //                 });
                //             }
                //         }) : a.setData({
                //             authorize: !0
                //         });
                //     }
                // })
                );
            },
            fail: function(t) {
                console.log(t), "getLocation:fail 1" == t.errMsg ? (wx.showToast({
                    title: "请确认您已打开手机定位，如未打开，请打开后再次进入小程序",
                    icon: "none",
                    duration: 3e3,
                    mask: !0
                }), a.setData({
                    refresh: !0
                })) : a.setData({
                    getlocation: !0,
                    location: !1
                }), console.log(a.data.location);
            },
            complete: function(t) {
                console.log(t);
            }
        });
    },
    changeModalCancel: function(t) {
        console.log(t);
    },
    bindGetUserInfo: function(n) {
        i = this;
        a.bindGetUserInfo(n, i);
        var i = this;
        if (console.log(n.detail.userInfo), n.detail.userInfo) {
            var s = "";
            wx.getStorage({
                key: "pid",
                success: function(t) {
                    if (console.log(t.data), s = t.data, console.log(s), "" != s && void 0 != s) {
                        var a = wx.getStorageSync("openid");
                        e.check_user({
                            openid: a
                        }).then(function(t) {
                            console.log(t), 0 == t.errno && (t.data ? e.get_group_bing({
                                openid: a,
                                pid: s
                            }) : (e.get_group_bing({
                                openid: a,
                                pid: s
                            }), e.get_commoned_bing({
                                openid: a,
                                pid: s
                            })), wx.removeStorage({
                                key: "pid",
                                success: function(t) {
                                    console.log(t.data);
                                }
                            }));
                        });
                    }
                }
            });
            var c = {
                avatarUrl: n.detail.userInfo.avatarUrl,
                nickName: n.detail.userInfo.nickName,
                openid: wx.getStorageSync("openid"),
                sessionkey: wx.getStorageSync("sessionkey")
            };
            i.data.location && (c.location = i.data.location), t.inserUserdata(c).then(function(t) {
                i.setData({
                    authorize: !1
                }), o.globalData.is_first_enter = 2;
            });
        } else wx.showToast({
            title: "授权失败",
            icon: "none",
            duration: 2e3,
        }), i.setData({
            authorize: !1
        });
    },
  noBindGetUserInfo: function () {
    wx.showToast({
      title: "授权失败",
      icon: "none",
      duration: 2e3,
    }), this.setData({
      authorize: !1
    });
  },
    select: function() {
        this.setData({
            getlocation: !0
        });
    },
    getlocate: function() {
        var e = this;
        e.setData({
            getlocation: !1
        }), wx.getSetting({
            success: function(a) {
                a.authSetting["scope.userLocation"] || (a.authSetting = {
                    "scope.userInfo": !0,
                    "scope.userLocation": !0
                }, wx.getSystemInfo({
                    success: function(a) {
                        "android" == a.platform || "ios" != a.platform ? (console.log(a), wx.openSetting({
                            success: function(a) {
                                wx.showLoading({
                                    title: "加载中..."
                                }), a.authSetting["scope.userLocation"] ? wx.getLocation({
                                    type: "wgs84",
                                    success: function(a) {
                                        console.log(a);
                                        var n = {
                                            lat: a.latitude,
                                            lng: a.longitude,
                                            types: 20
                                        };
                                        e.getLocal(n.lat, n.lng);
                                        t.getNearestHead(n).then(function(t) {
                                            console.log(t), wx.hideLoading(), o.globalData.location = !0, wx.setStorage({
                                                key: "head_info",
                                                data: t.data.data[0]
                                            }), e.setData({
                                                recommend: t.data.data,
                                                location: !0,
                                                loadingwait: !1,
                                                showContent: !1,
                                                showPrevLoading: !1,
                                                showselect: !1
                                            });
                                        }), t.getTeamOpenid({
                                            openid: wx.getStorageSync("openid")
                                        }).then(function(t) {
                                            console.log(t), 0 == t.errno && "" != t.data.head_info && (wx.setStorageSync("head_info", t.data.head), 
                                            e.setData({
                                                active: t.data.head_info
                                            }), console.log(e.data.active));
                                        });
                                    },
                                    fail: function(t) {
                                        "getLocation:fail 1" == t.errMsg && (wx.showToast({
                                            title: "请确认您已打开手机定位，如未打开，请打开后再次进入小程序",
                                            icon: "none",
                                            duration: 3e3,
                                            mask: !0
                                        }), e.setData({
                                            refresh: !0
                                        }));
                                    },
                                    complete: function(t) {
                                        console.log(t);
                                    }
                                }) : (wx.showLoading({
                                    title: "加载中..."
                                }), wx.hideLoading(), wx.showToast({
                                    title: "请确认您已授权，并且微信已打开位置授权",
                                    icon: "none",
                                    duration: 2e3,
                                    mask: !0
                                }), e.setData({
                                    showselect: !0,
                                    showPrevLoading: !1
                                }));
                            }
                        })) : "ios" == a.platform && wx.openSetting({
                            success: function(a) {
                                wx.showLoading({
                                    title: "加载中..."
                                }), a.authSetting["scope.userLocation"] ? wx.getLocation({
                                    type: "wgs84",
                                    success: function(a) {
                                        var n = {
                                            lat: a.latitude,
                                            lng: a.longitude,
                                            types: 20
                                        };
                                        e.getLocal(n.lat, n.lng);
                                        t.getNearestHead(n).then(function(t) {
                                            wx.hideLoading(), o.globalData.location = !0, wx.hideLoading(), o.globalData.location = !0, 
                                            wx.setStorage({
                                                key: "head_info",
                                                data: t.data.data[0]
                                            }), e.setData({
                                                recommend: t.data.data,
                                                location: !0,
                                                loadingwait: !1,
                                                showContent: !1,
                                                showPrevLoading: !1,
                                                showselect: !1
                                            });
                                        }), t.getTeamOpenid({
                                            openid: wx.getStorageSync("openid")
                                        }).then(function(t) {
                                            console.log(t), 0 == t.errno && "" != t.data.head_info && (wx.setStorageSync("head_info", t.data.head), 
                                            e.setData({
                                                active: t.data.head_info
                                            }), console.log(e.data.active));
                                        });
                                    },
                                    fail: function(t) {
                                        "getLocation:fail 1" == t.errMsg && (wx.showToast({
                                            title: "请确认您已打开手机定位，如未打开，请打开后再次进入小程序",
                                            icon: "none",
                                            duration: 3e3,
                                            mask: !0
                                        }), e.setData({
                                            refresh: !0
                                        }));
                                    },
                                    complete: function(t) {
                                        console.log(t);
                                    }
                                }) : (wx.showLoading({
                                    title: "加载中..."
                                }), wx.hideLoading(), wx.showToast({
                                    title: "请确认您已授权，并且微信已打开位置授权",
                                    icon: "none",
                                    duration: 2e3,
                                    mask: !0
                                }), e.setData({
                                    showselect: !0,
                                    showPrevLoading: !1
                                }));
                            }
                        });
                    }
                }));
            }
        });
    },
    close: function() {
        this.setData({
            getlocation: !1,
            showPrevLoading: !1,
            showselect: !0
        });
    },
    tel: function(t) {
        var e = t.currentTarget.dataset.phone;
        wx.makePhoneCall({
            phoneNumber: e
        });
    },
    btn: function(e) {
        for (var a = this, n = e.currentTarget.dataset.id, i = a.data.recommend, s = 0; s < i.length; s++) if (i[s].m_id == n) {
            var c = {
                openid: wx.getStorageSync("openid"),
                head_openid: i[s].m_openid
            };
            console.log(c), o.globalData.m_openid = i[s].m_openid, t.selectHead(c).then(function(t) {
                console.log(t), 0 == t.errno ? (wx.setStorage({
                    key: "head_info",
                    data: t.data
                }), 10 == a.data.type ? wx.navigateBack({
                    delta: 1
                }) : (a.data.shareEnter, wx.reLaunch({
                    url: "../template/index"
                }))) : wx.showToast({
                    title: t.message,
                    icon: "none",
                    duration: 2e3
                });
            });
        }
    },
    change: function(t) {
        this.setData({
            value: t.detail
        });
    },
    onSearch: function(e) {
        var a = this.data.value, o = {
            openid: wx.getStorageSync("openid"),
            name: a
        };
        t.getHead(o).then(function(t) {
            console.log(t);
        });
    },
    city: function(t) {
        var e = t.currentTarget.dataset.id;
        wx.navigateTo({
            url: "../group/citys?city_name=" + this.data.city + "&&cid=" + e
        });
    },
    getLocal: function(e, a) {
        var n = this, i = "";
        t.GetSnMd5({
            lat: e,
            lng: a
        }).then(function(s) {
            if (1 == s.errno) n.setData({
                city: "获取失败"
            }); else {
                i = JSON.parse(s.data.data), console.log(i);
                var c = i.result.ad_info.province, l = i.result.ad_info.city, r = s.data.cid;
                o.globalData.city = l, o.globalData.cid = r, n.setData({
                    province: c,
                    city: l,
                    latitude: e,
                    longitude: a,
                    cid: r
                });
                var d = {
                    openid: wx.getStorageSync("openid"),
                    location: i.result.address_component.province + "," + i.result.address_component.city + "," + i.result.address_component.district
                };
                t.inserUserdata(d).then(function(t) {
                    console.log(t), 1 == t.errno && n.setData({
                        location: location
                    });
                });
            }
        }, "json");
    },
    searchCity: function(t) {
        console.log(t);
        var e = t.currentTarget.dataset.city, a = t.currentTarget.dataset.id;
        wx.navigateTo({
            url: "../group/search?act=selected&&city=" + e + "&&cid=" + a
        });
    },
    chooseCommunity: function(t) {}
});